<template>
  <el-dialog ref="catedialog" :title="dialog" :visible.sync="dialogVisible" width="45%" @close="handleClose">
    <el-form ref="cateForm" :model="cateForm" :rules="rules" label-width="100px" class="demo-dynamic" size="medium">
      <el-form-item prop="catename" label="分类名称">
        <el-col :span="20"><el-input v-model="cateForm.catename" /></el-col>
      </el-form-item>
      <el-form-item label="图标class" prop="icon">
        <el-col :span="8"><el-input v-model="cateForm.icon" /></el-col>
        <el-col :span=".5">
          <i ref="font" class="fa" :class="[iconClass]" aria-hidden="true" />
        </el-col>
        <el-col :span="12"><a class="fontsome" href="https://fontawesome.com.cn/v4/icons-ui/">具体参见fontawesome图标库</a></el-col>
      </el-form-item>
      <el-form-item label="排序大小"><template>
        <el-input-number v-model="cateForm.sort_num" size="mideum" :min="1" />
      </template></el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addcate, catedetail, editcate } from '@/api/cate'
export default {
  data() {
    return {
      dialog: '添加分类',
      cateForm: {
        catename: '',
        icon: '',
        sort_num: 10
      },
      rules: {
        icon: [{
          required: true, message: '请输入图标class', trigger: 'blur'
        },
        {
          validator:
            (rule, value, callback) => {
              // console.log(value)
              // console.log(this.$refs.font.innerHTML)
              this.$nextTick(
                () => {
                  if (this.$refs.font.offsetWidth) {
                    callback()
                  } else {
                    callback(new Error('请输入正确的字体图标'))
                  }
                }
              )
            }

        }
        ],
        catename: { required: true, message: '请输入分类名称', trigger: 'change' }
      },
      iconClass: '',
      dialogVisible: false
    }
  },
  watch: {
    cateForm: {
      deep: true, // 深度监视
      immediate: true, // 立刻执行，一进入页面handler就立刻执行一次
      handler(newValue) {
        // console.log(newValue.icon)
        this.iconClass = newValue.icon.includes('fa-') ? newValue.icon : 'fa-' + newValue.icon
      }
    }
  },
  methods: {
    async getdetail(id) {
      // console.log(id)
      console.log(id)
      if (id === 'add') {
        this.dialog = '添加分类'
        // console.log(1)
        this.dialogVisible = true
      } else {
        this.dialog = '编辑分类'
        const res = await catedetail(id)
        // console.log(res)
        this.dialogVisible = true
        this.cateForm = res
      }
    },
    submitForm() {
      this.$refs.cateForm.validate(async(valid) => {
        // console.log(this.cateForm)
        if (valid) {
          console.log(this.cateForm)
          if (this.dialog === '添加分类') {
            const res = await addcate(this.cateForm)
            console.log(res)
            if (res === '') {
              this.$message.success('添加成功')
              this.handleClose()
              // this.reload()
              this.$emit('reload')
            }
          } else {
            const res = await editcate(this.cateForm)
            console.log(res)
            if (res === '') {
              this.$message.success('修改成功')
              this.handleClose()
              this.$emit('reload')
            }
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose() {
      this.cateForm = {
        catename: '',
        icon: '',
        sort_num: 10
      }
      this.dialogVisible = false
      this.$refs.cateForm.resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
.fontsome {
  margin-left: 15px;
  &:hover {
    border-bottom: 1px solid #51a7ff;
    padding-bottom: 5px;
    color: #51a7ff;
  }
}
</style>
